package com.haidao.admin.component.configuration;

import com.haidao.admin.component.entity.Task;
import com.haidao.admin.component.service.TaskService;
import com.haidao.admin.component.web.job.ReportJob;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lyy
 */
@Slf4j
@Configuration
public class QuartzConfiguration {

    @Resource
    private TaskService taskService;

    @PostConstruct
    public void initJobs() {
        log.info("init jobs");
        List<Task> tasks = taskService.getAll();
        if (tasks != null && !tasks.isEmpty()) {
            for (Task t : tasks) {
                if (t.getState() == 1) {
                    taskService.removeJob(t.getTaskKey());
                    Map<String, Object> params = new HashMap<>();
                    params.put("code", t.getTaskKey());
                    params.put("taskId", t.getId());
                    taskService.addJob(t.getTaskKey(), t.getCron(), params, ReportJob.class);
                }
            }
        }
    }
}